﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MP.Core.Library
{
    public class FileUtil
    {
        public static long TotalLinesOfFile(String fileName)
        {
            if (System.IO.File.Exists(fileName))
                return System.IO.File.ReadAllLines(fileName).Length;
            return 0;
        }
        public static byte[] ReadByteArrayFromFile(string fileName)
        {
            byte[] buff = null;
            FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            long numBytes = new FileInfo(fileName).Length;
            buff = br.ReadBytes((int)numBytes);
            br.Close();
            fs.Close();
            return buff;
        }

        /// <summary>
        /// Lay tat ca thu muc con(ke ca thu muc cha)
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static List<string> GetAllDirectory(string dir)
        {
            List<string> lstDir = new List<string>();
            GetAllDirectory(dir, lstDir);
            return lstDir;
        }
        protected static void GetAllDirectory(string dir, List<string> lstDir)
        {
            string[] subDirs = Directory.GetDirectories(dir);
            lstDir.Add(dir);
            for (int i = 0; i < subDirs.Length; i++)
            {
                GetAllDirectory(subDirs[i], lstDir);
            }
        }

        /// <summary>
        /// Lay ta ca thu muc con rong(khong chua thu muc con nao khac trong do)
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static List<string> GetSubDirectory(string dir)
        {
            List<string> lstDir = new List<string>();
            GetSubDirectory(dir, lstDir);
            return lstDir;
        }
        protected static void GetSubDirectory(string dir, List<string> lstDir)
        {
            string[] subDirs = Directory.GetDirectories(dir);
            if (subDirs.Length == 0)
                lstDir.Add(dir);
            else
            {
                for (int i = 0; i < subDirs.Length; i++)
                {
                    GetSubDirectory(subDirs[i], lstDir);
                }

            }
        }

    }
}
